<template>
  <div>
    <!-- 面包屑导航 -->
    <el-breadcrumb separator-class="el-icon-arrow-right">
      <el-breadcrumb-item :to="{ path: '/welcome' }">首页</el-breadcrumb-item>
      <el-breadcrumb-item>订单管理</el-breadcrumb-item>
      <el-breadcrumb-item>订单列表</el-breadcrumb-item>
    </el-breadcrumb>
    <!-- 卡片区域 -->
    <el-card>
      <!-- 搜索区域 -->
      <el-row :gutter="20">
        <el-col :span="8">
          <el-input placeholder="请输入搜索的内容" clearable>
            <el-button icon="el-icon-search" slot="append"></el-button>
          </el-input>
        </el-col>
      </el-row>

      <!-- 订单列表区域 -->

      <el-table :data="orderList" style="width: 100%">
        <el-table-column type="index"></el-table-column>
        <el-table-column prop="order_number" label="订单编号">
        </el-table-column>
        <el-table-column prop="order_price" label="订单价格"> </el-table-column>
        <el-table-column prop="pay_status" label="是否付款">
          <template slot-scope="scope">
            <el-tag
              type="success"
              effect="dark"
              v-if="scope.row.pay_status === '1'"
              >已付款</el-tag
            >
            <el-tag type="danger" effect="dark" v-else>未付款</el-tag>
          </template>
        </el-table-column>
        <el-table-column prop="is_send" label="是否发货"> </el-table-column>
        <el-table-column prop="create_time" label="下单时间">
          <template slot-scope="scope">
            {{ scope.row.create_time | dataFormat }}
          </template>
        </el-table-column>
        <el-table-column label="操作">
          <template>
            <el-button
              icon="el-icon-edit"
              type="primary"
              size="mini"
              @click="showBox"
            ></el-button>
            <el-button
              icon="el-icon-location"
              type="success"
              size="mini"
              @click="showProcessBox"
            ></el-button>
          </template>
        </el-table-column>
      </el-table>
    </el-card>

    <!-- 分页控件区域 -->
    <el-pagination
      @size-change="handleSizeChange"
      @current-change="handleCurrentChange"
      :current-page="queryInfo.pagenum"
      :page-sizes="[5, 10, 15]"
      :page-size="queryInfo.pagesize"
      layout="total, sizes, prev, pager, next, jumper"
      :total="total"
    >
    </el-pagination>

    <!-- 修改地址对话框 -->
    <el-dialog
      title="修改地址"
      :visible.sync="addressVisible"
      width="50%"
      @close="addressDialogClosed"
    >
      <el-form
        :model="addressForm"
        :rules="addressFormRules"
        ref="addressFormRef"
        label-width="100px"
      >
        <el-form-item label="省市区/县" prop="address1">
          <!-- 省市区县选择器 -->
          <el-cascader
            v-model="addressForm.address1"
            :options="cityData"
            :props="{ expandTrigger: 'hover' }"
          ></el-cascader>
        </el-form-item>
        <el-form-item label="详细地址" prop="address2">
          <el-input v-model="addressForm.address2"></el-input>
        </el-form-item>
      </el-form>
      <span slot="footer" class="dialog-footer">
        <el-button @click="addressVisible = false">取 消</el-button>
        <el-button type="primary" @click="addressVisible = false"
          >确 定</el-button
        >
      </span>
    </el-dialog>

    <!-- 物流信息对话框 -->
    <!-- <template slot-scope="scope"> -->
    <el-dialog
      title="物流进度"
      :visible.sync="processVisible"
      width="50%"
      @close="processDialogClosed"
    >
      <!-- 物流进度timeline时间线 -->
      <el-timeline-item>
        <el-timeline-item
          v-for="(activity, index) in processInfo"
          :key="index"
          :timestamp="activity.time"
        >
          {{ activity.context }}
        </el-timeline-item>
      </el-timeline-item>
    </el-dialog>
    <!-- </template> -->
  </div>
</template>
<script>
import cityData from './citydata.js'
export default {
  data () {
    return {
      // 订单数据
      orderList: [],
      // 查询对象
      queryInfo: {
        query: '',
        pagenum: 1,
        pagesize: 10
      },
      // 总数据条数
      total: 0,
      // 修改地址对话框的显示/隐藏
      addressVisible: false,
      // 修改地址表单绑定数据
      addressForm: {
        address1: [],
        address2: ''
      },
      // 修改地址表单绑定规则
      addressFormRules: {
        address1: [
          { required: true, message: '请选择省市区/县', trigger: 'blur' }
        ],
        address2: [
          { required: true, message: '请填写详细地址', trigger: 'blur' }
        ]
      },
      // 城市数据
      // cityData: cityData,
      cityData,
      // 物流信息的显示/隐藏
      processVisible: false,
      // 物流信息数据
      processInfo: []
    }
  },
  methods: {
    async getOrderData () {
      const { data: res } = await this.$http.get('orders', {
        params: this.queryInfo
      })
      if (res.meta.status !== 200) {
        return this.$message.error('获取订单失败')
      }
      this.total = res.data.total
      this.orderList = res.data.goods
      console.log(this.orderList)
    },
    // 监听页面显示条数改变函数
    handleSizeChange (newSize) {
      this.queryInfo.pagesize = newSize
      this.getOrderData()
    },
    // 监听页数改变函数
    handleCurrentChange (newPage) {
      this.queryInfo.pagenum = newPage
      this.getOrderData()
    },
    // 展示修改地址的对话框
    showBox () {
      this.addressVisible = true
    },
    // 监听关闭dialog事件
    addressDialogClosed () {
      console.log('111')
      this.$refs.addressFormRef.resetFields()
    },
    // 展示物流信息的对话框
    async showProcessBox () {
      const { data: res } = await this.$http.get('/kuaidi/804909574412544580')
      if (res.meta.status !== 200) {
        return this.$message.error('获取物流失败')
      }
      this.processInfo = res.data
      this.processVisible = true
    },
    // 监听关闭process事件
    processDialogClosed () {}
  },
  created () {
    this.getOrderData()
  }
}
</script>
<style lang="less" scoped>
@import '../../plugins/timeline/timeline.css';
@import '../../plugins/timeline-item/timeline-item.css';
.el-card {
  margin-top: 20px;
}

.el-pagination {
  text-align: center;
  background-color: #fff;
  padding: 10px;
}
.el-cascader {
  width: 100%;
}
</style>
